var express = require('express');
var router = express.Router();
const {
    newsModel,
    categoryModel,
    articleModel,
    videoModel
} = require("../model/model")
/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

router.get('/users', async (req, res) => {
  const users = await userModel.find({})
  res.send(users)
})

//添加发布者
router.post('/user/add', async (req, res) => {
  await newsModel.create(req.body)
  res.send({
    code:200,
    msg:"添加成功"
  })
})
//获取发布者
router.get('/user/list', async (req, res) => {
  const news = await newsModel.find({})
  res.send({
    code:200,
    news
  })
})

//添加文章
router.post('/article/add', async (req, res) => {
  await articleModel.create(req.body)
  res.send({
    code:200,
    msg:"添加成功"
  })
})
//获取文章
router.get('/article/list', async (req, res) => {
  const article = await articleModel.find()
  res.send({
    code:200,
    article
  })
})

//添加视频
router.post('/video/add', async (req, res) => {
  await videoModel.create(req.body)
  res.send({
    code:200,
    msg:"添加成功"
  })
})

//获取视频
router.get('/video/list', async (req, res) => {
  const video = await videoModel.find()
  res.send({
    code:200,
    video
  })
})

//添加类别
router.post('/category/add', async (req, res) => {
  await categoryModel.create(req.body)
  res.send({
    code:200,
    msg:"添加成功"
  })
})
//获取类别
router.get('/category/list', async (req, res) => {
  const category = await categoryModel.find()
  res.send({
    code:200,
    category
  })
})



module.exports = router;
